package com.example.administrator.szb.activity.luyan;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.NamePhone;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.LuYanList;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.ShareUtils;
import com.example.administrator.szb.view.LuYanBaoMingPop;

import java.util.HashMap;
import java.util.Map;

public class LuYanDeatilActivity extends MVPBaseActivity {
    private int id;
    private ImageView img;
    private TextView tv_title;
    private TextView tv_status;
    private TextView tv_company;
    private TextView tv_type;
    private TextView tv_date;
    private TextView tv_time;
    private TextView tv_baoming;
    private TextView tv_address;
    private TextView user_name;
    private TextView tv_phone;
    private TextView tv_extra;
    private TextView tv_bottom;
    private TextView bar_title;
    private ImageView right_sc;
    private ImageView right_img;
    private LuYanList luYanList;
    private LinearLayout ll_phone;
    private boolean showPhone = false;
    private View ll_statusa;
    private TextView project_status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lu_yan_deatil);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
    id = getIntent().getIntExtra("id",0);
        ll_phone = findViewById(R.id.ll_phone);
        showPhone = getIntent().getBooleanExtra("showPhone",false);
        ll_statusa=findViewById(R.id.ll_statusa);
        project_status=findViewById(R.id.project_status);
        img = (ImageView) findViewById(R.id.img);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_status = (TextView) findViewById(R.id.tv_status);
        tv_company = (TextView) findViewById(R.id.tv_company);
        tv_type = (TextView) findViewById(R.id.tv_type);
        tv_date = (TextView) findViewById(R.id.tv_date);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_baoming = (TextView) findViewById(R.id.tv_baoming);
        tv_address = (TextView) findViewById(R.id.tv_address);
        user_name = (TextView) findViewById(R.id.user_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_extra = (TextView) findViewById(R.id.tv_extra);
        tv_bottom = (TextView) findViewById(R.id.tv_bottom);
        bar_title = (TextView) findViewById(R.id.bar_title);
        bar_title.setText("路演详情");
        right_sc = (ImageView) findViewById(R.id.right_sc);
        right_sc.setVisibility(View.GONE);
        right_img = (ImageView) findViewById(R.id.right_img);
        if (showPhone){
            tv_bottom.setVisibility(View.GONE);
            ll_phone.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void initEvent() {
        right_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (luYanList==null){
                    return;
                }
                ShareUtils.share(LuYanDeatilActivity.this,null,URLAddress.LUYAN_URL+id+"&app=1",luYanList.getTitle(),luYanList.getExtras());
            }
        });
        tv_bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (luYanList==null){
                    return;
                }
                CheckInfoUtils.check(activity, iosDiaolog, tv_bottom, new CheckInfoUtils.OnSuccessLietener() {
                    @Override
                    public void onSuccess() {
                        if (luYanList.getIs_baoming()==1){
                            DialogUtil.showToast(context,"您已报过名");
                            return;
                        }
                        if (luYanList.getUser_id()==SPUtils.getUserId()){
                            DialogUtil.showToast(context,"不能报名自己的");
                            return;
                        }
                        showPop();
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {
        getInfo();
    }
    private void getInfo(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("uid", SPUtils.getUserId());
        HttpUtils.post(activity,URLAddress.LUYAN_GETDEATIL, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                luYanList  = JSON.parseObject(str,new TypeReference<LuYanList>(){});
                if (luYanList.getUser_id()==SPUtils.getUserId()){
                    tv_bottom.setVisibility(View.GONE);
                }
                tv_title.setText(luYanList.getTitle());
                Glide.with(context).load(URLAddress.HOST_PORT+luYanList.getImg()).placeholder(R.mipmap.lyan_test).error(R.mipmap.lyan_test).into(img);
                tv_company.setText(luYanList.getCompany_name());
                tv_type.setText(luYanList.getType_name());
                String[] times = luYanList.getStart_time().split(" ");
                String[] times2 = luYanList.getEnd_time().split(" ");
                tv_date.setText(times[0]);
                tv_time.setText(times[1]);
                tv_baoming.setText(times2[0]);

                if (TextUtils.isEmpty(luYanList.getCity())){
                    luYanList.setCity("");
                }
                if (TextUtils.isEmpty(luYanList.getAddress())){
                    luYanList.setAddress("");
                }

                tv_address.setText(luYanList.getCity()+luYanList.getAddress());
                user_name.setText(luYanList.getContacts());
                tv_phone.setText(luYanList.getTel());
                if (TextUtils.isEmpty(luYanList.getExtras())){
                    luYanList.setExtras("无");
                }

                tv_extra.setText(luYanList.getExtras());
                if (luYanList.getIs_baoming()==1){
                    tv_bottom.setText("已报名");
                    changeColor(0);
                }else {
                    tv_bottom.setText("报名");
                    changeColor(1);
                }
                switch (luYanList.getStatus()){
                    case 0:
                        tv_status.setBackgroundColor(Color.parseColor("#FFD1D1"));
                        tv_status.setTextColor(Color.parseColor("#FF4040"));
                        tv_status.setText("待审核");
                        changeColor(0);
                        break;
                    case 1:
                        tv_status.setBackgroundColor(Color.parseColor("#FFD1D1"));
                        tv_status.setTextColor(Color.parseColor("#FF4040"));
                        tv_status.setText("报名中");

                        break;
                    case 2:
                        tv_status.setBackgroundColor(Color.parseColor("#A2A8B6"));
                        tv_status.setTextColor(Color.parseColor("#FFFFFF"));
                        tv_status.setText("未通过");
                            ll_statusa.setVisibility(View.VISIBLE);
                            project_status.setText(luYanList.getMark());


                        changeColor(0);
                        break;
                    case 3:
                    case 4:
                        tv_status.setBackgroundColor(Color.parseColor("#A2A8B6"));
                        tv_status.setTextColor(Color.parseColor("#FFFFFF"));
                        tv_status.setText("已结束");
                        tv_bottom.setText("报名已结束");
                        changeColor(0);

                        break;
                }


            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
    private void changeColor(int n){
        if(n==0){
            tv_bottom.setBackgroundColor(Color.parseColor("#999999"));
            tv_bottom.setTextColor(Color.parseColor("#ffffff"));
            tv_bottom.setEnabled(false);
        }else {
            tv_bottom.setBackgroundColor(Color.parseColor("#0096FF"));
            tv_bottom.setTextColor(Color.parseColor("#ffffff"));
        }
    }
    String tel;
    String names;
    public void showPop(){
        Map<String,Object> map = new HashMap<>();
        map.put("uid",SPUtils.getUserId());
        HttpUtils.post(activity,URLAddress.USERS_GETTELNAME, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                NamePhone namePhone = JSON.parseObject(str,new TypeReference<NamePhone>(){});
                 tel = namePhone.getTel();
                names = namePhone.getReal_name().get(0);
                if (namePhone.getReal_name().size()>1){
                    names+=namePhone.getReal_name().get(1);
                }
                LuYanBaoMingPop.showInfo(LuYanDeatilActivity.this, tv_bottom, names, tel, new LuYanBaoMingPop.OnEditResultListener() {
                    @Override
                    public void onResult(String uname, String phone) {
                        baomingreq( uname, phone);
                    }
                });
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }

    private void baomingreq(String name,String tel){
        Map<String,Object> map = new HashMap<>();
        map.put("uid",SPUtils.getUserId());
        map.put("luyan_id",id);
        map.put("user_name",name);
        map.put("user_tel",tel);
        HttpUtils.post(activity,URLAddress.LUYAN_BAOMING, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                tv_bottom.setText("已报名");
                tv_bottom.setEnabled(false);
                LuYanBaoMingPop.hide(0);
                LuYanBaoMingPop.showSuccess(LuYanDeatilActivity.this, tv_bottom);
            }

            @Override
            public void onFailure(int requestCode, String str) {
                LuYanBaoMingPop.hide(0);
                DialogUtil.showToast(context,str);
            }
        });
    }
}
